package com.yimiyisu.contract.events;

import com.google.common.eventbus.Subscribe;
import com.yimiyisu.contract.events.model.CheckExpiredLoopEventModel;
import com.zen.ZenData;
import com.zen.ZenEngine;
import com.zen.ZenResult;
import com.zen.annotation.Crontab;
import com.zen.annotation.Inject;
import com.zen.interfaces.IEvent;
import com.zen.kit.ConfigKit;

import java.util.List;

// 每日凌晨3点执行
@Crontab("1 1 3 * * ? *")
//@Crontab("1/5 * * * * ? *")
public class CheckExpiredLoopEvent implements IEvent<CheckExpiredLoopEventModel> {
    @Inject
    private ZenEngine zenEngine;

    @Override
    @Subscribe
    public void execute(CheckExpiredLoopEventModel checkExpiredLoopEventModel) {
        // 每次查20条，循环执行
        boolean hasMore;
        do hasMore = expire();/**/ while (hasMore);
    }

    private boolean expire() {
        ZenResult result = zenEngine.execute("list/company_expired", ZenData.create());
        List<CheckExpiredLoopEventModel> list = result.asList(CheckExpiredLoopEventModel.class);
        if (list == null || list.isEmpty()) return false;
        for (CheckExpiredLoopEventModel model : list) {
            zenEngine.execute("patch/company",ZenData.create("id",model.getId()).put("status",0));
            ConfigKit.setTenantExpire(model.getId(), true);
        }
        return true;
    }
}
